package xyz.kuailemao.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.kuailemao.constant.CrowdConstant;
import xyz.kuailemao.entity.po.MemberPO;
import xyz.kuailemao.service.api.MemberService;
import xyz.kuailemao.util.ResultEntity;

/**
 * 创建时间：2022/10/11 14:44
 */
@RestController
public class MemberProviderHandler {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO) {

        try {
            memberService.saveMemeber(memberPO);

            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }
            return ResultEntity.failed(e.getMessage());
        }

    }


    @RequestMapping("/get/memberpo/by/login/acct/remote")
    public ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginacct") String loginAcct) {

        try {
            // 1.调用本地Service组件执行查询
            MemberPO memberPO = memberService.getMemberPOByLoginAcct(loginAcct);

            // 2.如果没有抛出异常则返回成功结果
            return ResultEntity.successWithData(memberPO);
        } catch (Exception e) {
            e.printStackTrace();
            // 3.如果出现异常则返回失败结果
            return ResultEntity.failed(e.getMessage());
        }

    }
}
